package com.example.findmystuff;

import com.database.findmystuff.DB;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Set;
import com.database.findmystuff.User;

public class MainActivity extends Activity {
	
	Button submitLogin, submitRegister;
	EditText mEmail;
	EditText mPassword;
	boolean check,isAdmin;
	public String checkE,checkP;
	public static DB database;
	public static String loggedUser;
	public Set<User> userList;
	public Context c = this;
	Toast toast;
	/**
	 * This method is called when the activity is created
	 * @param savedInstanceState a bundle used to save the state that this instance of the activity is in.
	 */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	database=new DB(this);
    	database.loadSerializedObject(database);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        submitLogin = (Button) findViewById(R.id.loginButton);
        submitRegister = (Button) findViewById(R.id.registerButton);
        mEmail = (EditText) findViewById(R.id.emailText);
        mPassword = (EditText) findViewById(R.id.pwText);
        
        if(database.getUserList().size()==0){
        	User newUser = new User("admin", "admin");
            newUser.setIsAdmin(true);
        	database.createUser(newUser);
        	database.saveObject(database);
        }
   
        submitLogin.setOnClickListener(
        		new View.OnClickListener(){
					@Override
					public void onClick(View v){
						check = checkLogin(mEmail.getText().toString(),mPassword.getText().toString());
						if (check == true && !isAdmin) {
							loggedUser=mEmail.getText().toString();
							toast = Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
							toast.show();
							Intent j = new Intent(MainActivity.this, HomeActivity.class);
							startActivity(j);
						}else if(check==true && isAdmin){
							loggedUser=mEmail.getText().toString();
							toast = Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
							toast.show();
							Intent j = new Intent(MainActivity.this, AdminHomeActivity.class);
							startActivity(j);
						}else{
							//
							return;
						}
					}
				});
        
        submitRegister.setOnClickListener(
        		new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent i = new Intent(MainActivity.this, RegisterActivity.class);
						startActivity(i);
					}
				});
    }
    
    /**
     * Checks the login information with the dummy credentials stated above.
     * 
     * @param e The email entered into the login page.
     * @param p The password entered into the login page.
     */
    public boolean checkLogin(String e,String p){
    	userList=database.getUserList();
    	for(User u:userList){
    		if(e.equals(u.getEmail())){
    			if(p.equals(u.getPassword())){
    				if(u.getIsAdmin()){
    					isAdmin=true;
    					u.setAttemptCounter(0);
    					return true;
    				}
    				if(database.getUser(e).getAttemptCounter()>=3){
    					toast = Toast.makeText(getApplicationContext(), e+" has been locked out. Please contact an Admin.", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
						toast.show();
    					return false;
    				}else{
    					Log.v("loginAttempt", "Login Successful!");
    					u.setAttemptCounter(0);
    					return true;
    				}
    			}else{
    				database.getUser(mEmail.getText().toString()).increaseCounter();
    				if(database.getUser(e).getAttemptCounter()>=3){
    					toast = Toast.makeText(getApplicationContext(), e+" has been locked out. Please contact an Admin.", Toast.LENGTH_SHORT);
    					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
    					toast.show();
    				}else{
    					toast = Toast.makeText(getApplicationContext(), "Password Incorrect! "+(3-database.getUser(e).getAttemptCounter())+" attempts remaining.", Toast.LENGTH_SHORT);
    					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
    					toast.show();
    				}
    				return false;
    			}
    		}else{
    			//
    		}
    	}
		toast = Toast.makeText(getApplicationContext(), "Account not Registered!", Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
		toast.show();
    	return false;
    }
    /**
     * Initializes the contents of the Activity's standard options menu.
     * @param menu the standard options menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
    	// Android rules
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}
